var seletedMap = new AUIMap();// 复选框分页选中的值
var PATH_PREFIX = "";

function setPathPrefix(path) {
	PATH_PREFIX = path;
}
$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	// 注册清空事件
	WASP_WIDGET.registerResetClearEvent();
});

var $treFeeTypeList = $('#treFeeTypeList');
// 按钮动作类型
var actionType = "";
// 列表按钮访问路径
var actionUrl = "";
// 功能按钮
var WASP_FEETYPE = {
	// 设置列表按钮访问路径
	setActionUrl : function(actionType, feeCode) {
		actionUrl = PATH_PREFIX + "treFeeTypeView.do?actionType=" + actionType + "&feeCode=" + feeCode;
		return actionUrl;
	},
	// 打开新增页面
	addView : function() {
		actionType = "add";
		window.open(WASP_FEETYPE.setActionUrl(actionType));
	},
	// 费用类型详情信息
	detailView : function(feeCode) {
		actionType = "detail";
		window.open(WASP_FEETYPE.setActionUrl(actionType, feeCode));
	},
	// 费用类型修改信息
	updateView : function(feeCode) {
		actionType = "update";
		window.open(WASP_FEETYPE.setActionUrl(actionType, feeCode));
	},
	// 费用类型停用信息
	deleteView : function(feeCode) {
		swal({
			title : "您确定要停用该费用类型信息?",
			text : "停用后不可恢复!",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#fc6821",
			confirmButtonText : "是",
			cancelButtonText : "否",
			closeOnConfirm : false,
			closeOnCancel : true
		}, function(isConfirm) {
			if (isConfirm) {
				var actionUrl = PATH_PREFIX + "deleteTreFeeType.do";
				$.ajax({
					type : 'POST',
					url : actionUrl,
					dataType : 'json',
					data : {
						feeCode : feeCode
					},
					success : function(data) {
						if (data.success) {
							swal("停用成功!", "", "success");
						} else {
							swal("停用失败!", "", "error");
						}
						queryByCondtion(true);
					},
					error : function(xhr) {
						sweetAlertAjaxError(xhr);
					}
				});
			}
		});
		queryByCondtion(true);
	},
	// 批量停用费用类型信息
	batchDeleteFeeType : function() {
		var feeCodes = seletedMap.keySet().toString();
		if (feeCodes.length == 0) {
			ctools.alert('请至少选择一条数据!', "", "warning");
			return;
		}

		swal({
			title : "您确定要停用选中的费用类型信息?",
			text : "停用后不可恢复!",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#fc6821",
			confirmButtonText : "是",
			cancelButtonText : "否",
			closeOnConfirm : false,
			closeOnCancel : true
		}, function(isConfirm) {
			if (isConfirm) {
				var actionUrl = PATH_PREFIX + "deleteTreFeeType.do";
				$.ajax({
					type : 'POST',
					url : actionUrl,
					dataType : 'json',
					data : {
						feeCode : feeCodes
					},
					success : function(data) {
						if (data.success) {
							swal("停用成功!", "", "success");
						} else {
							swal("停用失败!", "", "error");
						}
						queryByCondtion(true);
					},
					error : function(xhr) {
						sweetAlertAjaxError(xhr);
					}
				});
			}
		});
		queryByCondtion(true);
	}
};

$(function() {
	$treFeeTypeList.jqGrid({
		url : PATH_PREFIX + 'treFeeTypeListPage.do',
		datatype : "json",
		colNames : [ "费用编码", "费用名称", "feeLevel","费用层级", "费用缩写", "费用标准名称", "费用描述", "createId","经办人", "status","状态","操作" ],
		colModel : [ 
				{ name : 'feeCode', index : 'feeCode',width : 30, resizable : true, sortable : false , key : true}, 
				{ name : 'feeName', index : 'feeName', width : 80, resizable : true, sortable : false }, 
				{ name : 'feeLevel', index : 'feeLevel', hidden : true }, 
				{ name : 'feeLevelName', index : 'feeLevelName', width : 50, resizable : true, sortable : false }, 
				{ name : 'feeAbbr', index : 'feeAbbr', width : 50, resizable : true, sortable : false },
				{ name : 'feeStandardName', index : 'feeStandardName', width : 50, resizable : true, sortable : false }, 
				{ name : 'feeDesc', index : 'feeDesc', width : 50, resizable : true, sortable : false }, 
				{ name : 'createId', index : 'createId', hidden : true }, 
				{ name : 'createName', index : 'createName', width : 50, resizable : true, sortable : false }, 
				{ name : 'status', index : 'status', hidden : true }, 
				{ name : 'statusName', index : 'statusName', width : 50, resizable : true, sortable : false }, 
				{ name : 'option', index : 'option', width : 80, resizable : true, sortable : true } 
			],
		rowNum : 20,
		rowList : [ 20, 30, 50 ],
		multiselect : true,
		rownumbers : true,
		rownumWidth : 50,
		prmNames : {
			search : "search",
			page : "pageNo",
			rows : "limit"
		},
		height : 'auto',
		width : false,
		autowidth : true,
		editurl : '',
		viewrecords : true,
		cellEdit : false,
		shrinkToFit : true,
		grouping : false,
		jsonReader : {
			root : "items", // 结果集
			records : "total", // 总记录数
			total : "pageCount", // 总页数
			page : "pageNo", // 当前页
			repeatitems : false
		},
		onSelectRow : function(rowid, status) {
			if (status) {// 选中则保存ID，未选中则停用
				var rowData = $treFeeTypeList.jqGrid('getRowData', rowid);
				seletedMap.set(rowid, rowData);
			} else {
				seletedMap.remove(rowid);
			}
		},
		onSelectAll : function(rowids, status) {
			for (var i = 0; i < rowids.length; i++) {
				if (status) {// 选中则保存ID，未选中则停用
					var rowData = $treFeeTypeList.jqGrid('getRowData', rowids[i]);
					seletedMap.set(rowids[i], rowData);
				} else {
					seletedMap.remove(rowids[i]);
				}
			}
		},
		pager : "#treFeeTypeListPage",
		viewrecords : true,
		hidegrid : false,
		gridComplete : function() {
			var ids = $treFeeTypeList.jqGrid('getDataIDs');
			for (var i = 0; i < ids.length; i++) {
				var rowId = ids[i];
				var rowData = $treFeeTypeList.jqGrid('getRowData', rowId);
				var status = rowData.status;
				seletedMap.each(function(selectRowId) {
					if (selectRowId == rowId) {
						$treFeeTypeList.setSelection(rowId, false);
					}
				});
				detailBtn = '<a class="btn btn-link btn-jqgrid" title="详情" onclick="WASP_FEETYPE.detailView(\'' + rowId + '\');" >详情</a>';
				updateBtn = '';
				deleteBtn = '';
				if(status=='C'){
					updateBtn = '<span class="split"></span><a class="btn btn-link btn-jqgrid" title="修改" onclick="WASP_FEETYPE.updateView(\'' + rowId + '\');" >修改</a>';
					deleteBtn = '<span class="split"></span><a class="btn btn-link btn-jqgrid" title="停用" onclick="WASP_FEETYPE.deleteView(\'' + rowId + '\');" >停用</a>';
				}
				$treFeeTypeList.jqGrid('setRowData', ids[i], {
					option : detailBtn + updateBtn + deleteBtn
				});
			}
		}
	});
	$treFeeTypeList.navGrid('#treFeeTypeListage', {
		edit : false,
		add : false,
		del : false,
		search : false,
		refreshstate : 'current'
	});
	$treFeeTypeList.jqGrid('setFrozenColumns');
	jqGridResize($treFeeTypeList);
});
/**
 * 通过条件进行搜索
 */
function queryByCondtion(flag) {
	seletedMap.clear();
	// 产品简称
	var feeCode = $.trim($("#q-feeCode").val());
	var feeName = $.trim($("#q-feeName").val());
	var status = $.trim($("#q-status").val());
	var feeLevel = $.trim($("#q-feeLevel").val());

	var postData = $treFeeTypeList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
		'sp[feeCode]' : feeCode,
		'sp[feeName]' : feeName,
		'sp[status]' : status,
		'sp[feeLevel]' : feeLevel
	});
	if (flag) {
		$treFeeTypeList.trigger("reloadGrid", [ {
			page : 1
		} ]); // 重新载入Grid表格
	} else {
		$treFeeTypeList.trigger("reloadGrid"); // 重新载入Grid表格
	}
}
// 绑定回车事件,查询
$("body").keypress(function(event) {
	var key = event.which;
	if (key == "13") {
		queryByCondtion(true);
	}
});